package com.oauth.global.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.LazyPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.CommonResources;
import com.oauth.components.client.ui.HasWidgetInfo;
import com.oauth.components.client.ui.footer.FooterUI;
import com.oauth.components.client.ui.header.HeaderUI;
import com.oauth.facebook.client.ui.FacebookInfo;
import com.oauth.global.client.beans.ApplicationClientBean;
import com.oauth.global.client.rpc.GlobalRemoteService;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;
import com.oauth.global.client.ui.ManageInfo;
import com.oauth.global.client.ui.WidgetMenu;
import com.oauth.global.client.ui.WidgetMenuClickListener;
import com.oauth.global.client.ui.WidgetMenuItem;
import com.oauth.global.client.ui.WidgetPanel;
import com.oauth.global.client.ui.birthday.BirthdayWidgetInfo;
import com.oauth.global.client.ui.status.StatusWidgetInfo;
import com.oauth.google.client.ui.docs.DocsInfo;
import com.oauth.google.client.ui.orkut.OrkutInfo;
import com.oauth.google.client.ui.picasa.PicasaInfo;
import com.oauth.google.client.ui.youtube.YoutubeInfo;
import com.oauth.linkedin.client.ui.LinkedinInfo;
import com.oauth.twitter.client.ui.TwitterInfo;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class LocalEntryPoint implements EntryPoint {

	private final GlobalRemoteServiceAsync globalService = GWT.create(GlobalRemoteService.class);

	private static LocalEntryPointUiBinder uiBinder = GWT.create(LocalEntryPointUiBinder.class);

	interface LocalEntryPointUiBinder extends UiBinder<DockLayoutPanel, LocalEntryPoint> {
	}

	@UiField
	HeaderUI header;

	LazyPanel panel;

	@UiField
	FooterUI footer;

	@UiField
	FlowPanel centerPanel;

	@UiField
	WidgetMenu widgetMenu;

	@UiField
	WidgetPanel widgetPanel;

	Map<Long, HasWidgetInfo> widgetMap = new HashMap<Long, HasWidgetInfo>();

	public void onModuleLoad() {
		CommonResources.INSTANCE.css().ensureInjected();
		DockLayoutPanel p = uiBinder.createAndBindUi(this);

		header.setUserName(DOM.getElementById("headerEmailId").getInnerText());
		widgetMap.put(1l, (HasWidgetInfo) GWT.create(TwitterInfo.class));
		widgetMap.put(2l, (HasWidgetInfo) GWT.create(OrkutInfo.class));
		widgetMap.put(3l, (HasWidgetInfo) GWT.create(LinkedinInfo.class));
		widgetMap.put(4l, (HasWidgetInfo) GWT.create(FacebookInfo.class));
		widgetMap.put(5l, (HasWidgetInfo) GWT.create(DocsInfo.class));
		widgetMap.put(6l, (HasWidgetInfo) GWT.create(PicasaInfo.class));
		widgetMap.put(7l, (HasWidgetInfo) GWT.create(YoutubeInfo.class));

		GlobalRemoteServiceAsync.INSTANCE
				.getMyApplications(new OAuthAsnyCallBack<List<ApplicationClientBean>>() {
					public void onSuccess(List<ApplicationClientBean> result) {
						Long id = null;
						boolean found = false;
						String show = Window.Location.getParameter("show");
						if (show != null) {
							id = Long.parseLong(show);
						}
						if (result != null && result.size() > 0) {
							widgetMenu.addMenuSeparator();

							for (ApplicationClientBean application : result) {
								HasWidgetInfo w = widgetMap.get(application.getId());
								WidgetMenuItem menuItem = new WidgetMenuItem(application.getName(),
										w.getIcon());
								System.out.println(w.getIcon());
								menuItem.addClickHandler(new WidgetMenuClickListener(w, widgetPanel));
								widgetMenu.addMenu(menuItem);
								if (w.getId() == id) {
									found = true;
									widgetPanel.setWidget(w);
									w.init();
								}
							}
						}
						if (!found) {
							// Default set to manage widgets.
							HasWidgetInfo hasWidgetInfo = widgetMap.get(-1l);
							widgetPanel.setWidget(hasWidgetInfo);
							hasWidgetInfo.init();
						}
					}
				});

		addManageMenu();
		addGlobalMenus();
		validateShowParam();
		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(p);
		// RootPanel.get().add(dockPanel);
	}

	private void validateShowParam() {
		String show = Window.Location.getParameter("show");
		if (show != null) {
			Long id = Long.parseLong(show);
			HasWidgetInfo hasWidgetInfo = widgetMap.get(id);
			widgetPanel.setWidget(hasWidgetInfo);
			hasWidgetInfo.init();
		}
	}

	private void addGlobalMenus() {
		addStatusMenu();
		addBirthdayMenu();
	}

	private void addStatusMenu() {
		StatusWidgetInfo statusInfo = GWT.create(StatusWidgetInfo.class);
		statusInfo.setWidgetMap(widgetMap);
		WidgetMenuItem menuItem1 = new WidgetMenuItem(statusInfo);
		menuItem1.addClickHandler(new WidgetMenuClickListener(statusInfo, widgetPanel));
		widgetMenu.addMenu(menuItem1);
	}

	private void addBirthdayMenu() {
		BirthdayWidgetInfo birthdayInfo = GWT.create(BirthdayWidgetInfo.class);
		birthdayInfo.setWidgetMap(widgetMap);
		WidgetMenuItem menuItem1 = new WidgetMenuItem(birthdayInfo);
		menuItem1.addClickHandler(new WidgetMenuClickListener(birthdayInfo, widgetPanel));
		widgetMenu.addMenu(menuItem1);
	}

	private void addManageMenu() {
		ManageInfo browse = GWT.create(ManageInfo.class);
		WidgetMenuItem menuItem1 = new WidgetMenuItem(browse);
		browse.setMaps(widgetMap);
		menuItem1.addClickHandler(new WidgetMenuClickListener(browse, widgetPanel));
		widgetMenu.addMenu(menuItem1);
		widgetMenu.addMenuSeparator();
	}

}
